package com.itany.crawler;

import com.itany.crawler.proxy.MyProxyManager;
import com.itany.utils.StringUtils;
import org.apache.http.*;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;

public abstract class UrlBaseHandle {
    String url = "test-url";
    boolean proxyed = false;


    public UrlBaseHandle() {
    }

    public UrlBaseHandle(String url, boolean proxyed) {
        this.url = url;
        this.proxyed = proxyed;
    }

    public UrlBaseHandle(boolean proxyed) {
        this.proxyed = proxyed;
    }

    public boolean isProxyed() {
        return proxyed;
    }

    public void setProxyed(boolean proxyed) {
        this.proxyed = proxyed;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String doGet() throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = "";
        HttpGet get = new HttpGet(getUrl());
        HttpHost proxy = null;
        try {
            if (proxyed) {
                proxy = MyProxyManager.getHost();
                System.out.println("代理IP :" + proxy);
                RequestConfig config = RequestConfig.custom()
                        .setProxy(proxy)
                        .setConnectTimeout(10000)
                        .setSocketTimeout(10000)
                        .setConnectionRequestTimeout(10000)
                        .build();
                get.setConfig(config);

            }
            addHeader(get);
            response = httpClient.execute(get);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity, Charset.defaultCharset());
            }
            if (StringUtils.isEmpty(result)) {
                throw new CustomException("返回体为空");
            }
            MyProxyManager.incProxy(proxy);

        } catch (Exception e) {
            MyProxyManager.decProxy(proxy);
            throw e;
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    private void addHeader(HttpGet get) {
        get.setHeader("Cache-Control", "no-cache");
        get.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36 Edg/92.0.902.84");
    }

    public abstract String handle(Urls urls) throws Exception;

    @Override
    public String toString() {
        return "UrlBaseHandle{" +
                "url='" + url + '\'' +
                ", proxyed=" + proxyed +
                '}';
    }
}
